package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;
//执行关联查询
public class JDBCDemo5 {
    public static void main(String[] args) {
        try(
                Connection connection = DBUtil.getConnection();
                ){
            Statement statement = connection.createStatement();
            String sql = "SELECT t.id,t.name,t.salary,t.title,c.name,c.floor "+
                    "FROM teacher t "+
                    "JOIN class c ON t.id=c.teacher_id "+
                    "WHERE t.name='王克晶'   ";
            System.out.println(sql);
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
             /*
                String tName = resultSet.getString("t.name");
                int tSalary = resultSet.getInt("t.salary");
                String tTitle = resultSet.getString("t.title");
                String cName = resultSet.getString("c.name");
                int cFloor = resultSet.getInt("c.floor");
                System.out.println(tName+" "+tSalary+" "+tTitle+" "+cName+" "+cFloor);
              */
                //获取字段值时可以使用字段索引，注意：从1开始为结果集第一个字段。可读性差，不推荐
                String tName = resultSet.getString(2);
                int tSalary = resultSet.getInt(3);
                String tTitle = resultSet.getString(4);
                String cName = resultSet.getString(5);
                int cFloor = resultSet.getInt(6);
                System.out.println(tName+" "+tSalary+" "+tTitle+" "+cName+" "+cFloor);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }
}
